if(isPc()){
	//location.href = sy.path + "/web/";
}
$(function(){
	if(sy.areaCode != undefined){
		$("#cityName").html(sy.areaName);
		//init();
		//hasNews();
		module107();
	}
});

//热门商品
var indexGoods = sy.getModule('myApp',['infinite-scroll']);

indexGoods.directive('onFinishRenderFilters', function ($timeout) {
    return {
        restrict: 'A',
        link: function(scope, element, attr) {
            if (scope.$last === true) {
                $timeout(function() {
                    scope.$emit('ngRepeatFinished',attr.onFinishRenderFilters);
                });
            }
        }
    };
});

indexGoods.controller('indexController', function($scope,$http) {
  $scope.list = [];
  $scope.busy = false;
  $scope.page = 1;
  $scope.limit = 10;
  $scope.hasMore = true;
  
  $scope.focusList = [];
  $scope.cates = [];
  $scope.actList = [];
  $scope.loadFocus = function(){
	  sy.openLoad();
      $http.post(sy.path + "/m/i/focusList",{
    	  		areaCode:sy.areaCode,location:1
      		})
			.success(function (data) {
				console.log(data)
				if (data){
					$scope.focusList = data.content;
				}
	            sy.closeLoad();
	        }).error(function(data, status) {
	        	 
	        	sy.closeLoad();
			});
  };
  
  $scope.$on('ngRepeatFinished', function (event,data) {
		if(data == 'focus'){
			var swiper = new Swiper('.swiper-container', {
		        pagination: '.swiper-pagination',
		        autoplay : 3000,
		        speed:500,
		        paginationClickable: true,
		        loop:true
		    });
		}
	});
  
  //焦点图
  $scope.loadFocus();
  $scope.clickFocus = function(focus){
	  focusRedirect(focus.redirectType,focus.redirectContent);
  };
  
  $scope.loadCate = function(){
	  sy.openLoad();
      $http.post("/m/i/cateList",{
    	  		areaCode:sy.areaCode,location:1
      		})
			.success(function (data) {
				if (data.length > 0){
					$scope.cates = data;
				}
	            sy.closeLoad();
	        }).error(function(data, status) {
	        	 
	        	sy.closeLoad();
			});
  };
  //推荐分类
  $scope.loadCate();
  //跳转分类
  $scope.toCate = function(cate){
	  if(!cate) return;
	  location.href='/m/category?select='+cate.cateCode;
  };
  
  $scope.toCateList = function(){
	location.href='/m/category';  
  };
  
  $scope.loadAct = function(){
	  sy.openLoad();
	  console.log(sy.areaCode);
      $http.post("/m/i/activity",{
    	  		areaCode:sy.areaCode,location:1
      		})
			.success(function (data) {
				if (data){
					console.log(data);
					$scope.actList = data.content;
				}
	            sy.closeLoad();
	        }).error(function(data, status) {
	        	 
	        	sy.closeLoad();
			});
  };
  $scope.loadAct();
  
  $scope.act = function(act){
	  if (act){
		  if (act.type == 1)
			  location.href='/m/actstore/'+act.id;
		  else
			  location.href='/m/actgoods/'+act.id;
	  }else{
		  alert('敬请期待');
	  }
  };
  
  $scope.qianggou = function(){
	  location.href='/m/qianggou';
  };
  
  $scope.nextPage = function() {
	  if(!sy.areaCode) return;
	  //if ($scope.busy || !$scope.hasMore || sy.areaCode == undefined) return;
	  //$scope.busy = true;
      sy.openLoad();
      $http.post(sy.path + "/m/i/guess",{
    	  	lat : sessionStorage.geoLat,
			lng : sessionStorage.geoLng
      		})
			.success(function (data) {
				//if(data.length < $scope.limit)$scope.hasMore = false;
				$.each(data,function(i,o){
					if (o.distance){
						o.distance = parseInt(o.distance);
					}
					$scope.list.push(o);
				});
				//$scope.busy = false;
				//$scope.page += 1;
	            sy.closeLoad();
	        }).error(function(data, status) {
	        	 
	        	sy.closeLoad();
			});
  };
  
  $scope.cb = function(){
	  $scope.nextPage();
  }
  
  if(sessionStorage.geoLat == undefined){
	  getCurrentPosition($scope.cb);
  }else{
	  $scope.nextPage();
  }
  
  $scope.toDetail = function(g){
	  location.href = sy.path + "/m/goods/"+g.goodsId;
  }
});


//焦点图跳转
function focusRedirect(type,value){
	switch (type) {
	case 1:
		location.href = sy.path + "/www?url="+escape(value);
		break;
	case 2:
		location.href = sy.path +value;
		break;
	case 3:
		location.href = sy.path + '/m/goods/'+value;
		break;
	case 4:
		location.href = sy.path + '/m/store/'+value;
		break;
	case 5:
		location.href = sy.path + '/m/cateAct/'+value;
		break;
	case 6:
		//location.href = sy.path + '/m/vipActGoods/'+value;
		location.href = sy.path + '/m/indexAct/'+value;
		break;
	default:
		break;
	}
}


//限时抢购(唯一) 107
function module107(){
	var url = sy.path + "/m/i/quickshopping";
	sy.openLoad();
	$.post(url, {areaCode:sy.areaCode}, function(result) {
		console.log(result);
		sy.closeLoad();
		if(result){
			var appElement = document.querySelector('[ng-controller=indexController]');//获得绑定controllerdom节点
			var $scope = angular.element(appElement).scope(); //获得$scope对象
			$scope.qianggouImg = result.imgs[0];
			$scope.$apply();
			
			//var html = "<h2 class=\"i_one_t\">限时抢购</h2>";
			var html = '';
			html += "<b>距开始</b>";
			html += "<i>00</i><em>:</em>";
			html += "<i>00</i><em>:</em>";
			html += "<i>00</i>";
			
			/*$.each(result.imgs,function(i,o){
				html += "<img src=\""+sy.path+"/download?id="+o+"\" />";
			});*/
			$(".t_qq").html(html);
			if(result.seconds){
				seconds = result.seconds;
				minutes = result.minutes;
				
				$("#quick").on("click",function(){
					location.href = sy.path + "/m/qianggou";
				});
				
/*				//var html = "<h2 class=\"i_one_t\">限时抢购</h2>";
				var html = '';
				html += "<b>开始</b>";
				html += "<i>00</i><em>:</em>";
				html += "<i>00</i><em>:</em>";
				html += "<i>00</i>";
				
				$.each(result.imgs,function(i,o){
					html += "<img src=\""+sy.path+"/download?id="+o+"\" />";
				});
				$(".t_qq").html(html);*/
				
				setInterval("timer()", 1000);
			}else{
				
			}
		}else{
			 
		}
	}, 'json');
}

var seconds = 0;
var minutes = 0;
function timer(){
	var time = 0;
	--seconds;
	if(seconds > 0){//未开始
		time = formatTime(seconds);
	}else{
		var s = minutes*60+seconds;
		time = formatTime(s);
	}
	
	var html = "";
	html += "<b>"+(seconds>0?"距开始":"距结束")+"</b>";
	html += "<i>"+time.substring(0,2)+"</i><em>:</em>";
	html += "<i>"+time.substring(3,5)+"</i><em>:</em>";
	html += "<i>"+time.substring(6,8)+"</i>";
	
	$(".t_qg").html(html);
}

function formatTime(s){
    // 计算
    var h=0,i=0;
    if(s>60){
        i=parseInt(s/60);
        s=parseInt(s%60);
        if(i > 60) {
            h=parseInt(i/60);
            i = parseInt(i%60);
        }
    }
    // 补零
    var zero=function(v){
        return (v>>0)<10?"0"+v:v;
    };
    return [zero(h),zero(i),zero(s)].join(":");
};

function hasNews(){
	$.post(sy.path + "/m/i/hasNews", {}, function(result) {
    	if (result.code == 0) {
		}else{
			$("#hasNews").show();
		}
	}, 'json');
}